﻿using System.ComponentModel.Composition;
using SS.Common.DBBuilder.DBDataStructure;
using System.IO;
using SS.Net.AppGen.Generators;
using SS.Net.AppGen.Utils;

namespace SS.Net.AppGen.WebGenerators.WebControllerGenerator
{
    /// <summary>
    /// The generator is responsible for generating admin controller class
    /// </summary>
    [Export(typeof(AdminControllerGenerator))]
    public sealed class AdminControllerGenerator : BaseGenerator, IWebComponentsGenerator
    {
        #region Ctors

        /// <summary>
        /// The constructor with image
        /// </summary>
        public AdminControllerGenerator()
        {
            this.DirPath = Path.Combine(this.GetAppPath(), typeof(WebGenerator).Name, "Controllers");
            this.InitializeDir(this.DirPath);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The method is responsible for generating all partials
        /// </summary>
        /// <param name="db">Data source for generating</param>
        public void Generate(DataBase db)
        {
            foreach (Table tbl in db.Tables)
            {
                // Skips a Table if it is existent in Skip array
                if (!this.IsItemExistent(BaseConsts.SkipTables, tbl.Name))
                {
                    string imgAttr;
                    PartialControllerGenerator Generator;

                    if (tbl.IsUserTable())
                    {
                        Generator = IoC.Get<PartialControllerUserGenerator>();
                    }
                    else if (tbl.IsTableWithImage(out imgAttr))
                    {
                        GlobalVariables.ImageAttr = imgAttr;
                        Generator = IoC.Get<PartialControllerImageGenerator>();
                    }
                    else
                    {
                        Generator = IoC.Get<PartialControllerCommonGenerator>();
                    }

                    // Start to generate contents
                    if (Generator != null)
                    {
                        Generator.DirPath = this.DirPath;
                        Generator.GenerateContents(tbl);
                    }
                }
            }
        }

        #endregion
    }
}
